<?php

namespace app\controllers\wx;

use app\models\system\SetWx;
use app\controllers\Controller;

/**
 * Class WxController
 * @package app\controllers
 */
class ConnectionController extends Controller
{
    /**
     * connection wx
     *
     * 1) api url
     * 2) download mp-xxx.text
     * 3) set wx form
     * http://api.xxx.com/wx/connection/index
     */
    public function actionIndex()
    {
        if (isset($_GET['echostr'])) {
            if ($this->checkSignature()) {
                echo $_GET['echostr'];
                exit;
            }
        }

        $this->responseMessage();
    }

    private function checkSignature()
    {
        $signature = trim($this->get('signature', ''));
        $timestamp = trim($this->get('timestamp', ''));
        $nonce = trim($this->get('nonce', ''));

        $set = SetWx::getLastOne(false);
        $token = $set->token;

        $tmpArr = [$token, $timestamp, $nonce];
        sort($tmpArr);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        return $tmpStr == $signature;
    }

    private function responseMessage()
    {

    }
}